<template>
  <div
    class="avatar-container"
    @mouseenter="onMouseEnterAvatar"
    @mouseleave="onMouseLeaveAvatar"
    @click.prevent=$event.preventDefault();
  >
    <el-upload
      class="avatar-uploader"
      action=""
      :show-file-list="false"
    >
      <img src="../../assets/logo.png" class="avatar" />
    </el-upload>

    <div
      v-if="showUserInfo"
      class="user-info-popup"
      @mouseenter="onMouseEnterPopup"
      @mouseleave="onMouseLeavePopup"
    >
      <el-card class="box-card" style="width: 200px">
        <div slot="header" class="clearfix popup-header" >
          <img src="../../assets/logo.png" class="popup-avatar" />
        </div>
        <div class="text item" style="font-family: '楷体';padding: 10px; border-radius: 5px;">
          <div style="font-weight: bold; margin-bottom: 10px; text-align: center">用户信息</div>
          <div style="margin-top: 10px"><span style="font-weight: bold;">ID:</span> {{ this.$store.state.id }}</div>
          <div style="margin-top: 10px"><span style="font-weight: bold;">用户名:</span> {{ this.$store.state.username }}</div>
          <div style="margin-top: 10px"><span style="font-weight: bold;">手机:</span> {{ this.$store.state.phone }}</div>
          <div style="margin-top: 10px"><span style="font-weight: bold;">积分:</span> {{ this.$store.state.points }}</div>
        </div>
      </el-card>
      <div class="btn-group">
        <el-button round @click="confirm">确定</el-button>
        <el-button round @click="quit">退出登录</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import router from "@/router";
import axios from "axios";

export default {
  name:"AvatarView",
  data() {
    return {
      avatarUrl: "../assets/logo.png",
      showUserInfo: false,
      hideTimeout: null,
      items: [
        { id: "" },
        { username: "" },
        { phone: "" },
        { points: "" },
      ],
    };
  },
  methods: {
    getUerInfo(){
      axios.get(`/api/user/only123`).then(
          resp=>{
            this.$store.commit("SET_USERNAME",resp.data.username);
            this.$store.commit("SET_ID", resp.data.id);
            this.$store.commit("SET_PHONE", resp.data.phone);
            this.$store.commit("SET_POINTS", resp.data.points);
          }
      ).catch(error=>{
        console.error('Error fetching data: ', error);
      })
    },
    onMouseEnterAvatar() {
      this.getUerInfo();
      clearTimeout(this.hideTimeout);
      this.showUserInfo = true;
    },
    onMouseLeaveAvatar() {
      this.hideWithDelay();
    },
    onMouseEnterPopup() {
      clearTimeout(this.hideTimeout);
    },
    onMouseLeavePopup() {
      this.hideWithDelay();
    },
    hideWithDelay() {
      this.hideTimeout = setTimeout(() => {
        this.showUserInfo = false;
      }, 300); // 设置300毫秒的延迟
    },
    confirm() {
      this.hideWithDelay();
    },
    quit() {
      router.replace('/login');
    },
  },
  mounted() {
    this.getUerInfo();
  },
};
</script>

<style scoped>
.avatar-container {
  display: inline-block;
  position: relative;
}

.avatar-uploader {
  display: flex;
  width: 40px;
  height: 40px;
  border: 1px dashed #d9d9d9;
  border-radius: 50%;
  position: relative;
  overflow: hidden;
  cursor: pointer;
  justify-content: center;
  align-items: center;
}

.avatar {
  width: 100%;
  height: 100%;
  display: block;
  border-radius: 50%;
}

.user-info-popup {
  position: absolute;
  top: 50px;
  left: 0;
  background: #fff;
  padding: 10px;
  border: 1px solid #ccc;
  z-index: 9999;
}

.popup-header {
  display: flex;
  justify-content: center;
  align-items: center;
}

.popup-avatar {
  width: 80px;
  height: 80px;
  border-radius: 50%;
}

.btn-group {
  display: flex;
  justify-content: space-between;
  margin-top: 10px;
}
</style>
